<!-- 人工草地生物量 -->
<template>
	<view class="container">
		<view class="example">
			<!-- 基础用法，不包含校验规则 -->
			<uni-forms ref="baseForm" class="form-box" :model="surveyBrush" labelWidth="105px">
				<uni-forms-item label="样方面积">
					<uni-easyinput v-model="surveyBrush.yfmj" :disabled="isview" placeholder="请输入   ㎡" />
				</uni-forms-item>
				<uni-forms-item label="灌丛名称">
					<uni-easyinput v-model="surveyBrush.gmmc" :disabled="isview" placeholder="请输入 " />
				</uni-forms-item>
				<uni-forms-item label="长度">
					<uni-easyinput v-model="surveyBrush.changdu" :disabled="isview" placeholder="请输入 cm" />
				</uni-forms-item>
				<uni-forms-item label="宽度">
					<uni-easyinput v-model="surveyBrush.kuaidu" :disabled="isview" placeholder="请输入 cm" />
				</uni-forms-item>
				<uni-forms-item label="盖度">
					<uni-easyinput v-model="surveyBrush.gaidu" :disabled="isview" placeholder="请输入 %" />
				</uni-forms-item>
				<uni-forms-item label="株从数">
					<uni-easyinput v-model="surveyBrush.zcs" :disabled="isview" placeholder="请输入 " />
				</uni-forms-item>
				<uni-forms-item label="单株从">
					<uni-easyinput v-model="surveyBrush.dzc" :disabled="isview" placeholder="请输入 " />
				</uni-forms-item>
				<uni-forms-item label="平均产量">
					<uni-easyinput v-model="surveyBrush.pjcl" :disabled="isview" placeholder="请输入 g/m²" />
				</uni-forms-item>
				<uni-forms-item label="公顷产量">
					<uni-easyinput v-model="surveyBrush.gqcl" :disabled="isview" placeholder="请输入 kg/hm²" />
				</uni-forms-item>

				<uni-forms-item label="物候相">
					<uni-easyinput v-model="surveyBrush.whq" :disabled="isview" placeholder="请输入 " />
				</uni-forms-item>

				<uni-forms-item label="生活力">
					<uni-easyinput v-model="surveyBrush.shenghl" :disabled="isview" placeholder="请输入 " />
				</uni-forms-item>


			</uni-forms>
			<view class="btn-box">
				<button type="primary" @click="submit()">{{title}}</button>
				<!-- 	<button type="primary" @click="add" v-if="isview">新增</button> -->
			</view>
		</view>


	</view>
</template>

<script>
	import {
		//	, addOrUpdateBiomass
		addOrUpdateBrushSurvey
	} from '@/api/system.js'

	import {
		saveImage,
		uploadImage
	} from '@/core/file/File.js'
	import {
		getInfo,
		setInfo
	} from '@/core/unicommon/storyageInfo.js'
	export default {
		data() {
			return {
				title: "提交",
				isview: true,
				sourceTypeIndex: 2,
				sourceType: ['拍照', '相册', '拍照或相册'],
				sizeTypeIndex: 2,
				sizeType: ['压缩', '原图', '压缩或原图'],
				countIndex: 8,
				count: [1, 2, 3, 4, 5, 6, 7, 8, 9],
				surveyBrush: {
					"changdu": "",
					"dzc": "",
					"gaidu": "",
					"gaodu": "",
					"gmmc": "",
					"gqcl": "",
					"kuaidu": "",
					"pjcl": "",
					"shenghl": "",
					"whq": "",
					"ydNumber": "",
					"yfNumber": "",
					"yfmj": "",
					"zcs": ""
				},



				network: false,

				// 校验表单数据
				valiFormData: {
					name: '',
					age: '',
					introduction: '',
				},
				// 校验规则
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					age: {
						rules: [{
							required: true,
							errorMessage: '年龄不能为空'
						}, {
							format: 'number',
							errorMessage: '年龄只能输入数字'
						}]
					}
				},

			}
		},
		computed: {

		},
		onLoad(e) {

			if (e && e.data) {
				let routeData = JSON.parse(decodeURIComponent(e.data))
				console.log(routeData)
				if (e.type && e.type == "view") {
					this.surveyBrush = routeData
					this.title = "编辑"
					this.isview = true
				} else {

					this.surveyBrush.ydNumber = routeData.ydNumber
					this.surveyBrush.yfNumber = routeData.yfNumber
					this.title = "提交"
					this.isview = false
				}

			} else {
				uni.showToast({
					title: `未获取到相关样方信息`
				})
			}
			getInfo("userdata").then(res => {
				console.log(res.data)
				// let env = res.data.brush.find(t => t.yfNumber == this.surveyBrush.yfNumber && t
				// 	.ydNumber == this.surveyBrush.ydNumber)
				// if (env) {
				// 	this.title = "编辑"
				// 	this.isview = true
				// 	// this.surveyBrush = env
				// } else {
				// 	this.title = "提交"
				// 	this.isview = false
				// }
			})

			// getInfo("network").then(res => {
			// 	this.network = res.data
			// })
			this.getNetWork()

		},
		onReady() {
			this.getNetWork()
			// getInfo("network").then(res => {
			// 	this.network = res.data
			// })
		},
		methods: {
			getNetWork() {
				let that = this
				uni.getNetworkType({
					success: res => {
						if (res.networkType === 'none') {
							that.network = false
						} else {
							that.network = true
						}
					}
				})
			},

			add() {
				this.surveyBrush = this.createObj(this.surveyBrush)
				this.title = '提交'
				this.isview = false

			},
			createObj(obj) {
				return {

					"changdu": "",
					"dzc": "",
					"gaidu": "",
					"gaodu": "",
					"gmmc": "",
					"gqcl": "",
					"kuaidu": "",
					"pjcl": "",
					"shenghl": "",
					"whq": "",

					"yfmj": "",
					"zcs": "",
					"ydNumber": obj.ydNumber || "",
					"yfNumber": obj.yfNumber || "",
					action: "creat"

				}
			},


			submit(ref) {

				if (this.title == "提交") {
					uni.showLoading({
						title: "提交中",
						mask: true,
						icon: 'loading',
					})
					if (this.network) {
						this.surveyBrush.action = 'none'
					} else {
						if (this.surveyBrush.action == 'none') {
							this.surveyBrush.action = 'edit'
						}
					}
					this.saveLocal().then(res => {
						if (this.network) {
							addOrUpdateBrushSurvey(this.surveyBrush).then(res => {
								uni.hideLoading()
								uni.showToast({
									title: '提交成功',
									icon: 'checkmarkempty'
								})
							})
						} else {
							uni.hideLoading()
							uni.showToast({
								title: '无网络,数据缓存成功',
								icon: 'checkmarkempty'
							})
						}
						this.title = "编辑"
						this.isview = true
					})
				} else {
					this.title = "提交"
					this.isview = false
				}

			},
			async saveLocal() {

				let userdataRes = await getInfo("userdata")
				let userdata = userdataRes.data
				let that = this
				let grassTypes = userdata.brush
				if (this.surveyBrush.id) {
					let index = grassTypes.findIndex(t => t.id == this.surveyBrush.id)
					grassTypes[index] = this.surveyBrush
					userdata.brush = grassTypes
				} else {
					grassTypes.push(this.surveyBrush)
					userdata.brush = grassTypes
				}
				await setInfo("userdata", userdata)
				console.log("aaa", userdata)


			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		position: absolute;
		width: 100vw;
		height: 100vh;

	}

	.example {
		position: absolute;
		width: 100%;
		height: 100vh;
		overflow-y: auto;

		background-color: #fff;

		.btn-box {
			display: flex;
		}
	}

	.form-box {
		padding: 15px;
	}

	.segmented-control {
		margin-bottom: 15px;
	}




	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}

	.totopindex {
		// width: 100%;
		padding: 10rpx;
		margin: 5rpx;
		height: 60rpx;
		border: 1px solid #ccc;
		border-radius: 8px;

		.btns {
			text-align: center;

			line-height: 56rpx;
			color: #00aaff;


		}
	}
</style>